package eleanor.ltd.isRealEstate.pages.user;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import eleanor.ltd.isRealEstate.annotations.AdministratorAccess;
import eleanor.ltd.isRealEstate.dao.user.UserDAO;
import eleanor.ltd.isRealEstate.entities.user.User;
import eleanor.ltd.isRealEstate.model.MD5;

@AdministratorAccess
public class AddUser {

	@Property
	private String username;
	
	@Property
	private String password;
	
	@Property
	private String email;
	
	@Inject
	private UserDAO userDAO;
	
	@Component
	private Form addUserForm;
	
	@Log
	Object onSubmitFromAddUserForm() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		System.out.println(dateFormat.format(date));
		try {
			User user = new User();
			user.setUsername(username);
			user.setPassword(MD5.md5(password));
			user.setEmailAddress(email);
			user.setDateCreated(date);
			user.setActiveOrNot("yes");
			
			userDAO.save(user);
			return AddUser.class;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	
}
